// pages/manage/expand/add/add.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    screenHeight: app.globalData.screenHeight,
    publishData:{
      dealerName:'',//车商名称
      contactName:'',//联系人
      contactMobile:'',//联系电话
      dealerAddress:'',//车商地址
      stallNums:'',//摊位数
      carStocks:'',//车辆库存数
      picsUrl:'',
      remarks:''
    },
    picList:[],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.InitUserStatus();
  },
  // 检查一下登录状态。
  async InitUserStatus(){
      app.showLoading("获取用户信息");
      await app.checkToken();
      this.InitUserInfo();
      wx.hideLoading();
  },
  InitUserInfo(){
      // this.goQuery();
  },

  publishDataInput(e){
    let publishData = this.data.publishData;
    publishData[e.currentTarget.dataset.name] = e.detail.value;
    this.setData({
        'publishData':publishData
    });
    this.validSubParams();
  },
  validSubParams(){
    let yuan = {
      dealerName:'',//车商名称
      contactName:'',//联系人
      contactMobile:'',//联系电话
      dealerAddress:'',//车商地址
      stallNums:'',//摊位数
      carStocks:'',//车辆库存数
      picsUrl:'',
      remarks:''
    };
    let publishData = this.data.publishData;
    let canSub = true;
    for (const key in publishData) {
        if (Object.hasOwnProperty.call(publishData, key) && Object.hasOwnProperty.call(yuan, key)) {
            if ('picsUrl' != key) {
                if (yuan[key] == publishData[key]) {
                    canSub = false;
                }
            }
            
        }
    };
    this.setData({
        'canSub': canSub
    });
    return canSub;
  },
  carLoanClueAdd(){
    if (this.validSubParams()) {
        let chooseDealerPicList = this.selectComponent('#chooseDealerPicList');
        let that = this;
        chooseDealerPicList.uploadFiles('dealerData',0,true).then(res=>{
            let dealerPicList = [];
            res.forEach(element => {
                dealerPicList.push(element.originalUrl);
            });
            let publishData = this.data.publishData;
            publishData.picsUrl = dealerPicList.join(",");
            app.sHttp.custMagerDealerBisitAdd(publishData).then(res =>{
                let _that = that;
                wx.showModal({
                  title: '提交完成',
                  content: '您的拜访已提交完毕',
                  confirmText:'我知道了',
                  showCancel: false,
                  success (res) {
                    _that.BackPage();
                  }
                })
            },err=>{
               app.error(err.msg||err);
            })
        },err=>{
         app.error(err.msg||err);
        });
    }else{
        app.alert('请补全信息');
    }
  },
  BackPage() {
    var that = this;
    wx.navigateBack({
      delta: 1,
      fail: function(){
        that.toHome();
      }
    });
  },
  toHome(){
    wx.reLaunch({
      url: '/pages/home/home',
    })
  },
  doNothing(){
  }
})